package com.atguigu.gmall.all.controller;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.list.client.ListFeignClient;
import com.atguigu.gmall.model.list.SearchParam;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.*;

/**
 * @author Fxz
 * @version 1.0
 * @date 2022-02-14 19:27
 */
@SuppressWarnings("all")
@Controller
@RequiredArgsConstructor
public class ListController {

	private final ListFeignClient listFeignClient;

	@RequestMapping("list.html")
	public String listPage(SearchParam searchParam, Model model) {
		Result<Map> result = listFeignClient.list(searchParam);
		String urlParam = makeUrlParam(searchParam);
		String trademarkParam = makeTrademarkParam(searchParam.getTrademark());
		List<Map> propsParamList = makeProps(searchParam.getProps());
		Map<String, Object> orderMap = dealOrder(searchParam.getOrder());

		model.addAttribute("orderMap", orderMap);
		model.addAttribute("propsParamList", propsParamList);
		model.addAttribute("trademarkParam", trademarkParam);
		model.addAttribute("searchParam", searchParam);
		model.addAttribute("urlParam", urlParam);
		model.addAllAttributes(result.getData());
		return "list/index";
	}

	/**
	 * 获取用户的排序规则
	 */
	private Map<String, Object> dealOrder(String order) {
		Map<String, Object> map = new HashMap<>();
		if (StringUtils.isNotBlank(order)) {
			String[] split = order.split(":");
			if (ArrayUtils.isNotEmpty(split) && split.length == 2) {
				map.put("type", split[0]);
				map.put("sort", split[1]);
				return map;
			}
		}
		else {
			map.put("type", "1");
			map.put("sort", "desc");
		}
		return map;
	}

	/**
	 * 获取平台属性值面包屑
	 */
	private List<Map> makeProps(String[] props) {
		LinkedList<Map> list = new LinkedList<>();
		if (ArrayUtils.isNotEmpty(props)) {
			Arrays.stream(props).forEach(item -> {
				String[] split = item.split(":");
				if (ArrayUtils.isNotEmpty(split) && split.length == 3) {
					HashMap<String, String> map = new HashMap<>(3);
					map.put("attrId", split[0]);
					map.put("attrValue", split[1]);
					map.put("attrName", split[2]);
					list.add(map);
				}
			});
		}
		return list;
	}

	/**
	 * 获取品牌面包屑
	 */
	private String makeTrademarkParam(String trademark) {
		if (StringUtils.isNotBlank(trademark)) {
			String[] split = trademark.split(":");
			if (ArrayUtils.isNotEmpty(split) && split.length == 2) {
				return "品牌:" + split[1];
			}
		}
		return "";
	}

	/**
	 * 获取到请求路径中的参数
	 */
	private String makeUrlParam(SearchParam searchParam) {
		StringBuffer sb = new StringBuffer();
		if (StringUtils.isNotBlank(searchParam.getKeyword())) {
			sb.append("keyword=").append(searchParam.getKeyword());
		}
		if (ObjectUtils.isNotEmpty(searchParam.getCategory1Id())) {
			sb.append("category1Id").append(searchParam.getCategory1Id());
		}
		if (ObjectUtils.isNotEmpty(searchParam.getCategory2Id())) {
			sb.append("category2Id").append(searchParam.getCategory2Id());
		}
		if (ObjectUtils.isNotEmpty(searchParam.getCategory3Id())) {
			sb.append("category3Id").append(searchParam.getCategory3Id());
		}

		if (StringUtils.isNotBlank(searchParam.getTrademark())) {
			if (sb.length() > 0) {
				sb.append("&trademark=").append(searchParam.getTrademark());
			}
		}
		if (ArrayUtils.isNotEmpty(searchParam.getProps())) {
			if (sb.length() > 0) {
				Arrays.stream(searchParam.getProps()).map(item -> sb.append("&props=").append(item));
			}
		}

		return "list.html?" + sb.toString();
	}

}
